# Maintainer: Martell Malone <martellmalone@gmail.com>

_realname=conemu
_mingw_suff=mingw-w64-cross
pkgbase="${_mingw_suff}-${_realname}-git"
pkgname="${_realname}-git"
replaces=("${_realname}")
provides=("${_realname}")
conflicts=("${_realname}")
pkgver=r6331.f3e6679ef
pkgrel=1
pkgdesc="Customizable Windows terminal with tabs, splits, quake-style and more"
arch=('i686' 'x86_64')
url="https://conemu.github.io/"
license=('custom')
makedepends=("git" "${_mingw_suff}-gcc" "${_mingw_suff}-binutils" "${_mingw_suff}-crt")
# CAUTION:
# Don't remove `!strip' option!  Otherwise, 64-bit version of ConEmu will signal
# errors at startup.
options=('!strip' 'staticlibs' '!emptydirs' '!buildflags')
source=("${_realname}"::"git+https://github.com/Maximus5/ConEmu.git"
        "ConEmu.xml"
		"0001-ConEmuStart.cpp.patch"
		"0002-remove-new-delete.patch"
		"0003-min-max-uppercase.patch"
		"0004-CEStr.h.patch"
		"0005-ConEmuC.cpp.patch"
		"0006-MainThread.h.patch"
		"0007-ConEmuHk-makefile_gcc.patch")

sha256sums=('SKIP'
            '84c201cf676aae844a5c00b20ce8912f004562422a3e340fe01c786ab666062c'
			'78b22fef7fb05b7935c1f05fc5ebb9ac9c4c4472a21b4733a76b134fe776acfc'
			'bf0e05a25dbfce2b80065386977cd678bc0d72150e4358bc95c67a0d958df5d5'
			'0a595c5b37ce2c6f1da51092f5fdb178a9b6d1d6c7202b5d8ffca24b63f49696'
			'0ca73c5d4904d8cc29b146f5fa35bfd321d678a5b2997326e2f1fcdec8f66112'
			'441d825f50f0ac445bb4cafa50ad67b3a9841fe8daaa7173c7127d58d8819750'
			'b4e53e8b8f3c9790a9b9c0290506d62a4cad7809bb83e6639331786e7cccc038'
			'36e1bf488c74d96230275ad599569934f58e475beec29a396bcaffc3cef3ed72')

case "${CARCH}" in
  i686*)
    _primary_host=i686-w64-mingw32
    _secondary_host=x86_64-w64-mingw32
  ;;
  x86_64*)
    _primary_host=x86_64-w64-mingw32
    _secondary_host=i686-w64-mingw32
  ;;
esac

pkgver() {
  cd "${srcdir}/${_realname}"
  printf 'r%s.%s' "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd "${srcdir}/${_realname}"
  
  git submodule update --init --recursive 
  
  patch -p1 -i ${srcdir}/0001-ConEmuStart.cpp.patch
  patch -p1 -i ${srcdir}/0002-remove-new-delete.patch
  patch -p1 -i ${srcdir}/0003-min-max-uppercase.patch
  patch -p1 -i ${srcdir}/0004-CEStr.h.patch
  patch -p1 -i ${srcdir}/0005-ConEmuC.cpp.patch
  patch -p1 -i ${srcdir}/0006-MainThread.h.patch
  patch -p1 -i ${srcdir}/0007-ConEmuHk-makefile_gcc.patch
  
  local f
  for f in $(find src -path "src/modules" -prune -o -type f \( -name "*.cpp" -o -name "*.cc" -o -name "*.c" -o -name "*.h" \) -print | \
			 xargs grep -P '\bmin\s*\(|\bmax\s*\(' | \
			 grep -Po '^[^:]+' | \
			 sort -u); do
		sed -i -r -e 's/\bmin\s*\(/MIN\(/g' -e 's/\bmax\s*\(/MAX\(/g' ${f}
  done
  for f in $(find src -type f \( -name "*.cpp" -o -name "*.cc" -o -name "*.c" -o -name "*.h" \) | \
			 xargs grep -P '<[A-Z]\w+\.(H|h)>' | \
			 grep -Po '^[^:]+' | \
			 sort -u); do
		sed -i -r '/#include\s+<[A-Z]\w+\.(H|h)>/ s/[A-Z]/\l&/g' ${f}
  done
  sed -i -r -e 's/-lNetapi32/-lnetapi32/g' src/ConEmu/makefile_gcc
  sed -i -r 's/-lShlwapi/-lshlwapi/g' src/Setup/GenVersion/makefile_gcc
  sed -i -r 's/-lShlwapi/-lshlwapi/g' src/ConEmuC/makefile_gcc
  
  sed -i -r -e 's/-fno-rtti//g' \
			-e 's/-lKernel32/-lkernel32/g' \
			-e 's/-lUser32/-luser32/g' \
			src/makefile_gcc_inc
  sed -i -r -e 's/-lAdvapi32/-ladvapi32/g' \
			-e 's/-lGdi32/-lgdi32/g' \
			-e 's/-lShell32/-lshell32/g' \
			-e 's/-lShlwapi/-lshlwapi/g' \
			-e 's/-lOleAut32/-loleaut32/g' \
			src/ConEmuCD/makefile_gcc
  [ -f src/ConEmuC/ConEmuC.def ] || sed -i -r 's/^DEF\s*=.*/DEF =/g' src/ConEmuC/makefile_gcc
  sed -i 's/ConsoleMain3=ConsoleMain2@8/ConsoleMain3=ConsoleMain3@8/' src/ConEmuCD/export.gcc.def
  sed -i '/RequestTrampolines=/d' src/ConEmuHk/export.gcc.def
}

build() {
  cd "${_realname}/src"

  # NOTE:
  # The ugliest build system I've ever seen, yuck!
  # CAUTION:
  # Don't remove `-static*' switches from `USERLIBSFIRST'!  Otherwise, when
  # running `cmd.exe' (Command Prompt) in ConEmu, it cannot find
  # `libstdc++-6.dll' if it is not in the current `PATH' environment variable.

  for _target in ${_primary_host} ${_secondary_host}; do
    msg "Building ${_realname} for ${_target%%-*}"
	case "${_target}" in
		i[3-7]86-*) DIRBIT=32;;
		x86_64-*) DIRBIT=64;;
	esac
    [[ -d ${srcdir}/build-${_target} ]] && rm -rf ${srcdir}/build-${_target}
    cp -rf ${srcdir}/${_realname} ${srcdir}/build-${_target}
    cd ${srcdir}/build-${_target}/src
    CROSS_HOST="${_target}-" make -j1 \
	  DIRBIT=${DIRBIT} \
      -f makefile_all_gcc WIDE="1" \
      USERCPP="-pipe -O3 -fomit-frame-pointer -fpermissive -std=c++11" \
      USERLIBSFIRST="-static -static-libgcc -static-libstdc++"
  done
}

package() {
  mkdir -p "${pkgdir}/opt/bin"

  local _suffix=''

  for _target in ${_primary_host} ${_secondary_host}; do
    msg "Installing ${_realname} for ${_target%%-*}"
    cd "${srcdir}/build-${_target}/Release"
    if [ "${CARCH}" == "${_target%%-*}" ]; then
      cp -r "ConEmu.exe" "${pkgdir}/opt/bin/ConEmu.exe"
    fi
    if test "${_target%%-*}" == "x86_64"; then
      _suffix="64"
    else
      _suffix=""
    fi
    cp -r "ConEmu/ConEmuC${_suffix}.exe"  "${pkgdir}/opt/bin/ConEmuC${_suffix}.exe"
    cp -r "ConEmu/ConEmuCD${_suffix}.dll" "${pkgdir}/opt/bin/ConEmuCD${_suffix}.dll"
    cp -r "ConEmu/ConEmuHk${_suffix}.dll" "${pkgdir}/opt/bin/ConEmuHk${_suffix}.dll"
  done

  install -Dm644 ${srcdir}/ConEmu.xml "${pkgdir}"/opt/share/conemu/ConEmu.xml
}
